<template>
  <div>
    <div class="strategyFatherBox">
      <el-button type="success" size='small' @click="backupsDialogClick()" class="baconfig">{{$t('备份配置')}}</el-button>
      <el-button type="success" size='small' @click="recoveryDialogClick()" class="reconfig">{{$t('恢复配置')}}</el-button>
      <el-button type="success" size='small' @click="dialogVisible()" class="refactory">{{$t('恢复出厂设置')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="del">{{$t('删除')}}</el-button>
    </div>
    <el-table size='mini' ref="multipleTable" :data="configList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="filename" align='center' :label="$t('文件名')" min-width="270">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.filename" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="desc" align='center' :label="$t('描述')" min-width="300">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.desc" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column align='center' width='200' fixed="right" :label="$t('操作')">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="downloadFile(scope.row)" :class="'upload' + (scope.$index + 1)">{{$t('下载')}}</el-button>
          <el-button type="danger" plain size="mini" @click="reFile(scope.row)" :class="'reduc' + (scope.$index + 1)">{{$t('还原')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <DownloadDialog v-if="downloadDialogVisible" ref="refDownloadDialogVisible"></DownloadDialog>
    <BackupsDialog v-if="backupsDialogVisible" ref="refBackupsDialogVisible"></BackupsDialog>
    <RecoveryDialog v-if="recoveryDialogVisible" ref="refRecoveryDialogVisible"></RecoveryDialog>
  </div>
</template>
<script>

import { getBackupArray, backupDelInfo, backupReSet, backupReduction } from '@/api/system/configManager'
import DownloadDialog from './components/DownloadDialog'
import BackupsDialog from './components/BackupsDialog'
import RecoveryDialog from './components/RecoveryDialog'
export default {
  name: 'ConfigManager',
  components: {
    DownloadDialog,
    BackupsDialog,
    RecoveryDialog
  },
  data () {
    return {
      downloadDialogVisible: false,
      backupsDialogVisible: false,
      recoveryDialogVisible: false,
      configList: [],
      nameStr: ''
    }
  },
  async created () {
    await this.listData()
  },
  methods: {
    // 多选按钮
    handleSelectionChange (data) {
      var nameStr = ''
      for (var item of data) {
        if (nameStr === '') {
          nameStr = item.filename
        } else {
          nameStr = nameStr + '@#@' + item.filename
        }
      }
      this.nameStr = nameStr
    },
    // 列表
    async listData () {
      await getBackupArray().then(res => {
        if (res.status === 0) {
          this.configList = res.info
        }
      })
    },
    // 备份配置
    backupsDialogClick (data) {
      this.backupsDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refBackupsDialogVisible.init(data)
      })
    },
    // 恢复配置
    recoveryDialogClick (data) {
      this.recoveryDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refRecoveryDialogVisible.init(data)
      })
    },
    // 下载
    downloadFile (data) {
      this.downloadDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refDownloadDialogVisible.init(data)
      })
    },
    deleteData () {
      if (this.nameStr) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          backupDelInfo({ name: this.nameStr }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.nameStr = []
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    dialogVisible (data) {
      this.$confirm(this.$t('您是否要恢复出厂设置？恢复出厂设置设备会重启！'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        backupReSet().then(res => {
          if (res.status === 0) {
            this.$message({
              message: this.$t(res.msg),
              type: 'success'
            })
            this.listData()
          }
        })
      })
    },
    reFile (data) {
      this.$confirm(this.$t('确定要还原吗？还原需要设备重启！'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        backupReduction({ name: data.filename }).then(res => {
          if (res.status === 0) {
            this.$message({
              message: this.$t(res.msg),
              type: 'success'
            })
            this.listData()
          }
        })
      })
    }
  }
}
</script>
